#include "RedisConn.h"
#include <stdexcept>

using namespace std;
using namespace CwRedis;

bool RedisConn::connect(const char *ip, unsigned int port) {
    conn_ = redisConnect(ip, port);
    if (conn_ == nullptr || conn_->err) {
        return false;
    }
    return true;
}

bool RedisConn::connect(const RedisConnConfig &config) {
    if (!connect(config.getIp(), config.getPort())) {
        return false;
    }
    return !command("AUTH", config.getUser(), config.getPassword()).isError();
}

string RedisConn::getError() const {
    if (conn_ == nullptr) {
        throw runtime_error("empty ptr redis context");
    }
    return conn_->errstr;
}
